<?php


namespace App\Http\Model\Job;


use App\Enum\ClassAmtTransferEnum;
use Illuminate\Support\Facades\DB;

class PfsHeadAddbatchtransferModel
{

    /**
     * 查询监管账户划拨子编号是否已存在
     *
     * @param $pfsId
     * @param $subTransferId
     *
     * @return array|false|mixed
     */
    public static function transferListExist($pfsId, $subTransferId)
    {
        $exist = DB::select('SELECT `id` FROM `class_amt_transfer_detail` WHERE `pfs_dw_id`=:pfs_dw_id AND `sub_transfer_id`=:sub_transfer_id AND `result`=:result AND `is_del`=0 LIMIT 1', [
            'pfs_dw_id'       => $pfsId,
            'sub_transfer_id' => $subTransferId,
            'result'          => ClassAmtTransferEnum::RESULT_SUCCESS,
        ]);
        return empty($exist) ? [] : current($exist);
    }


    /**
     * 查询当日在本次划拨记录前是否已存在划拨记录
     *
     * @param $data
     *
     * @return array
     */
    public static function last($data)
    {
        $last = [];
        foreach ($data['list'] as $val) {
            $exist = DB::select('SELECT `id`,`pfs_transfer_status`,`result`,`transfer_amt`,`transfer_id` FROM `class_amt_transfer_detail` WHERE `pfs_dw_id`=:pfs_dw_id AND `create_date`=:create_date AND `class_id`=:class_id AND `is_del`=0 AND `transfer_id`!=:transfer_id ORDER BY `id` DESC LIMIT 1', [
                'pfs_dw_id'   => $data['pfs_dw_id'],
                'create_date' => $val->create_date,
                'class_id'    => $val->class_id,
                'transfer_id' => empty($data['transfer_id']) ? '' : $data['transfer_id'],
            ]);
            if (!empty($exist)) {
                $last[$val->class_id] = $exist;
            }
        }
        return $last;
    }


    /**
     * 招行-提出划拨/解冻>机构端>招行-入库
     *
     * @param $data
     * @param $result
     */
    public static function save($data, $result, $errMsg = '')
    {
        if (empty($data['transfer_id'])) {
            return 0;
        }
        $time = date('Y-m-d H:i:s');
        $list = [];
        /*获取招行接口响应结果*/
        if (isset($result->data->success) && $result->data->success === true) {
            $resultData = ClassAmtTransferEnum::RESULT_SUCCESS;
        } else {
            //$resultData = ClassAmtTransferEnum::RESULT_WAIT;
            $resultData = ClassAmtTransferEnum::RESULT_FAILURE;
        }
        $resultDesc = empty($result->message) ? '' : $result->message;
        $createDate = null;
        foreach ($data['list'] as $val) {
            if (empty($createDate)) {
                $createDate = $val->create_date;
            }
            $arr = [
                /*主体*/
                'ledger_subject_id'    => (string) $data['ledger_subject_id'],
                'ledger_subject_name'  => (string) $data['ledger_subject_name'],
                /*城市*/
                'city_name'            => (string) $data['city_name'],
                'area_code'            => (string) $data['area_code'],
                /*单据推送账号时间*/
                'push_time'            => $time,
                /*招行&数仓-业务线分校配置信息主键ID*/
                'pfs_dw_id'            => $data['pfs_dw_id'],
                /*招行接口同步结果: SUCCESS 成功、FAILURE 失败、UNKNOW 未知、ING 推送中、WAIT 未推送*/
                'result'               => $resultData,
                /*招行接口同步结果描述*/
                'result_desc'          => empty($resultDesc) ? $errMsg : $resultDesc,
                /*班级信息*/
                'class_name'           => $val->class_name,
                'class_id'             => $val->class_id,
                /*划拨申请编号*/
                'transfer_id'          => $data['transfer_id'],
                /*划拨申请子编号*/
                'sub_transfer_id'      => $val->sub_transfer_id,
                /*划拨金额*/
                'transfer_amt'         => $val->transfer_amt,
                /*销课时长，若划拨类型为销课模式，则此参数必传*/
                'eliminate_class_hour' => $val->eliminate_class_hour,
                /*本次划拨比率，如30.0, 37.5, 100 模式01必填*/
                'rate'                 => $val->rate,
                /*第几次划拨/解冻，如1,2,3 模式01必填*/
                'count'                => $val->count,
                /*数仓-划拨时间*/
                'transfer_date'        => $val->transfer_date,
                /*数仓-创建时间*/
                'create_date'          => $val->create_date,
            ];
            /*查询划拨记录是否已存在*/
            $exist = DB::select('SELECT `id` FROM `class_amt_transfer_detail` WHERE `pfs_dw_id`=:pfs_dw_id AND `transfer_id`=:transfer_id AND `sub_transfer_id`=:sub_transfer_id AND `create_date`=:create_date AND `is_del`=0 LIMIT 1', [
                'pfs_dw_id'       => $data['pfs_dw_id'],
                'transfer_id'     => $data['transfer_id'],
                'sub_transfer_id' => $val->sub_transfer_id,
                'create_date'     => $val->create_date,
            ]);
            if (empty($exist)) {
                /*如果记录不存在,则新增*/
                $list[] = $arr;
            } else {
                /*存在则更新*/
                DB::table('class_amt_transfer_detail')->where('id', $exist[0]->id)->update($arr);
            }
        }
        /*查询申请单据是否存在*/
        $exist = DB::select('SELECT `id` FROM `class_amt_transfer` WHERE `pfs_dw_id`=:pfs_dw_id AND `transfer_id`=:transfer_id AND `apply_date`=:apply_date AND `is_del`=0 LIMIT 1', [
            'pfs_dw_id'   => $data['pfs_dw_id'],
            'transfer_id' => $data['transfer_id'],
            'apply_date'  => $createDate,
        ]);
        $transfer = [
            /*主体*/
            'ledger_subject_id'   => (string) $data['ledger_subject_id'],
            'ledger_subject_name' => (string) $data['ledger_subject_name'],
            /*城市*/
            'city_name'           => (string) $data['city_name'],
            'area_code'           => (string) $data['area_code'],
            /*单据推送招行时间*/
            'push_time'           => $time,
            /*招行&数仓-业务线分校配置信息主键ID*/
            'pfs_dw_id'           => $data['pfs_dw_id'],
            /*招行接口同步结果: SUCCESS 成功、FAILURE 失败、UNKNOW 未知、ING 推送中、WAIT 未推送*/
            'result'              => $resultData,
            /*招行接口同步结果描述*/
            'result_desc'         => $resultDesc,
            /*划拨申请编号，系统内唯一*/
            'transfer_id'         => $data['transfer_id'],
            /*划拨申请课程数*/
            'transfer_num'        => $data['transfer_num'],
            /*本次申请划拨总金额*/
            'sum_transfer_amt'    => $data['sum_transfer_amt'],
            /*申请日期，格式：YYYY-MM-DD*/
            'apply_date'          => $createDate,
        ];
        if (empty($exist)) {
            /*如果记录不存在,则新增*/
            DB::table('class_amt_transfer')->insert($transfer);
        } else {
            /*更新*/
            DB::table('class_amt_transfer')->where('id', $exist[0]->id)->update($transfer);
        }
        if (!empty($list)) {
            DB::table('class_amt_transfer_detail')->insert($list);
        }
    }

}





/**
 * PfsHeadAddbatchtransferModel.php
 *
 * 说明:
 *
 * 修改历史
 * ----------------------------------------
 * 2021/6/8 16:19:00   操作:创建
 **/
